package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.entity.Address;
import com.shop.mapper.AddressMapper;
import com.shop.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("addressService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
public class AddressServiceImpl implements AddressService {
    /**
     * 注入mapper
     */
    private final AddressMapper addressMapper;

    /**
     * 分页查询地址
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Address> listAddress(Integer pageNum, Integer pageSize) {
        List<Address> list = addressMapper.listAddress(pageNum, pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public List<Address> listWithAddressMappers(Integer userId) {
        List<Address> list = addressMapper.listWithAddressMappers(userId);
        return new ArrayList<>(list);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public Address getAddressById(Integer id) {
        return addressMapper.Address(id);
    }

    /**
     * 添加地址
     * @param address
     */
    @Override
    public void addAddress(Address address) {
        addressMapper.addAddress(address);
    }

    /**
     * 删除地址
     * @param id
     * @param userId
     */
    @Override
    public void deleteAddress(Integer id) {
        addressMapper.deleteAddress(id);
    }

    /**
     * 修改地址
     * @param address
     */
    @Override
    public void updateAddress(Address address) {
        addressMapper.updateAddress(
                address.getId(),
                address.getUserId(),
                address.getReceiverName(),
                address.getPhone(),
                address.getProvinceCityDistrict(),
                address.getDetailAddress(),
                address.getIsDefault());
    }
    @Override
    public int getTotal() {
        return addressMapper.getTotal();
    }
}
